<!DOCTYPE html>
<html>
<head>
    

    

    



    <meta charset="utf-8">
    
    
    
    <title>Docker | 刺客 | 水不撩不知深浅，人不拼怎知输赢。</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="Docker">
    <meta name="description" content="Docker安装CentOS7安装Docker使用阿里云脚本安装／升级你的Docker客户端 1curl -sSL http://acs-public-mirror.oss-cn-hangzhou.aliyuncs.com/docker-engine/internet | sh - 如何使用Docker加速器去阿里Docker镜像仓库注册账号：https://cr.console.aliyun.c">
<meta name="keywords" content="Docker">
<meta property="og:type" content="article">
<meta property="og:title" content="Docker">
<meta property="og:url" content="http://koushen001.oschina.io/2017/08/17/Docker/index.html">
<meta property="og:site_name" content="刺客">
<meta property="og:description" content="Docker安装CentOS7安装Docker使用阿里云脚本安装／升级你的Docker客户端 1curl -sSL http://acs-public-mirror.oss-cn-hangzhou.aliyuncs.com/docker-engine/internet | sh - 如何使用Docker加速器去阿里Docker镜像仓库注册账号：https://cr.console.aliyun.c">
<meta property="og:updated_time" content="2017-08-18T09:19:31.049Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Docker">
<meta name="twitter:description" content="Docker安装CentOS7安装Docker使用阿里云脚本安装／升级你的Docker客户端 1curl -sSL http://acs-public-mirror.oss-cn-hangzhou.aliyuncs.com/docker-engine/internet | sh - 如何使用Docker加速器去阿里Docker镜像仓库注册账号：https://cr.console.aliyun.c">
    
        <link rel="alternate" type="application/atom+xml" title="刺客" href="/atom.xml">
    
    <link rel="shortcut icon" href="/favicon.ico">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@latest/css/style.css">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    

</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/img/brand.jpg)">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">刺客</h5>
          <a href="mailto:875569847@qq.com" title="875569847@qq.com" class="mail">875569847@qq.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                文章
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                标签
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/categories"  >
                <i class="icon icon-lg icon-th-list"></i>
                分类
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/koushen001" target="_blank" >
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">Docker</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="Search">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">Docker</h1>
        <h5 class="subtitle">
            
                <time datetime="2017-08-17T01:10:08.000Z" itemprop="datePublished" class="page-time">
  2017-08-17
</time>


	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/工具/">工具</a></li></ul>

            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#Docker安装"><span class="post-toc-number">1.</span> <span class="post-toc-text">Docker安装</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#CentOS7安装Docker"><span class="post-toc-number">1.1.</span> <span class="post-toc-text">CentOS7安装Docker</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#如何使用Docker加速器"><span class="post-toc-number">1.2.</span> <span class="post-toc-text">如何使用Docker加速器</span></a></li></ol></li><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#Docker镜像"><span class="post-toc-number">2.</span> <span class="post-toc-text">Docker镜像</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#获取Docker镜像"><span class="post-toc-number">2.1.</span> <span class="post-toc-text">获取Docker镜像</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#运行"><span class="post-toc-number">2.2.</span> <span class="post-toc-text">运行</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#列出镜像"><span class="post-toc-number">2.3.</span> <span class="post-toc-text">列出镜像</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#虚悬镜像"><span class="post-toc-number">2.4.</span> <span class="post-toc-text">虚悬镜像</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#中间层镜像"><span class="post-toc-number">2.5.</span> <span class="post-toc-text">中间层镜像</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#列出部分镜像"><span class="post-toc-number">2.6.</span> <span class="post-toc-text">列出部分镜像</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#利用-commit-理解镜像构成"><span class="post-toc-number">2.7.</span> <span class="post-toc-text">利用 commit 理解镜像构成</span></a></li></ol></li><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#undefined"><span class="post-toc-number">3.</span> <span class="post-toc-text">Hello, Docker!</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#使用-Dockerfile-定制镜像"><span class="post-toc-number">3.1.</span> <span class="post-toc-text">使用 Dockerfile 定制镜像</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#Dockerfile-指令详解"><span class="post-toc-number">3.2.</span> <span class="post-toc-text">Dockerfile 指令详解</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#FROM-指定基础镜像，一个-Dockerfile-中-FROM-是必备的指令，并且必须是第一条指令。"><span class="post-toc-number">3.2.1.</span> <span class="post-toc-text">FROM 指定基础镜像，一个  Dockerfile  中  FROM  是必备的指令，并且必须是第一条指令。</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#RUN-指令是用来执行命令行命令的。"><span class="post-toc-number">3.2.2.</span> <span class="post-toc-text">RUN  指令是用来执行命令行命令的。</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#COPY-复制文件"><span class="post-toc-number">3.2.3.</span> <span class="post-toc-text">COPY 复制文件</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#ADD-更高级的复制文件"><span class="post-toc-number">3.2.4.</span> <span class="post-toc-text">ADD 更高级的复制文件</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#CMD-容器启动命令"><span class="post-toc-number">3.2.5.</span> <span class="post-toc-text">CMD 容器启动命令</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#ENTRYPOINT-入口点"><span class="post-toc-number">3.2.6.</span> <span class="post-toc-text">ENTRYPOINT 入口点</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#ENV-设置环境变量"><span class="post-toc-number">3.2.7.</span> <span class="post-toc-text">ENV 设置环境变量</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#ARG-构建参数"><span class="post-toc-number">3.2.8.</span> <span class="post-toc-text">ARG 构建参数</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#VOLUME-定义匿名卷"><span class="post-toc-number">3.2.9.</span> <span class="post-toc-text">VOLUME 定义匿名卷</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#EXPOSE-声明端口"><span class="post-toc-number">3.2.10.</span> <span class="post-toc-text">EXPOSE 声明端口</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#WORKDIR-指定工作目录"><span class="post-toc-number">3.2.11.</span> <span class="post-toc-text">WORKDIR 指定工作目录</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#USER-指定当前用户"><span class="post-toc-number">3.2.12.</span> <span class="post-toc-text">USER 指定当前用户</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#HEALTHCHECK-健康检查"><span class="post-toc-number">3.2.13.</span> <span class="post-toc-text">HEALTHCHECK 健康检查</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#ONBUILD-为他人做嫁衣裳"><span class="post-toc-number">3.2.14.</span> <span class="post-toc-text">ONBUILD 为他人做嫁衣裳</span></a></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#删除本地镜像"><span class="post-toc-number">3.3.</span> <span class="post-toc-text">删除本地镜像</span></a></li></ol></li><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#Docker容器"><span class="post-toc-number">4.</span> <span class="post-toc-text">Docker容器</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#启动容器"><span class="post-toc-number">4.1.</span> <span class="post-toc-text">启动容器</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#后台-background-运行"><span class="post-toc-number">4.2.</span> <span class="post-toc-text">后台(background)运行</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#进入容器"><span class="post-toc-number">4.3.</span> <span class="post-toc-text">进入容器</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#导出和导入容器"><span class="post-toc-number">4.4.</span> <span class="post-toc-text">导出和导入容器</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#删除容器"><span class="post-toc-number">4.5.</span> <span class="post-toc-text">删除容器</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#清理所有处于终止状态的容器"><span class="post-toc-number">4.6.</span> <span class="post-toc-text">清理所有处于终止状态的容器</span></a></li></ol></li><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#Docker仓库"><span class="post-toc-number">5.</span> <span class="post-toc-text">Docker仓库</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#安装运行-docker-registry"><span class="post-toc-number">5.1.</span> <span class="post-toc-text">安装运行 docker-registry</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#在私有仓库上传、下载、搜索镜像"><span class="post-toc-number">5.2.</span> <span class="post-toc-text">在私有仓库上传、下载、搜索镜像</span></a></li></ol></li><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#Docker-数据管理"><span class="post-toc-number">6.</span> <span class="post-toc-text">Docker 数据管理</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#创建一个数据卷"><span class="post-toc-number">6.1.</span> <span class="post-toc-text">创建一个数据卷</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#删除数据卷"><span class="post-toc-number">6.2.</span> <span class="post-toc-text">删除数据卷</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#数据卷容器"><span class="post-toc-number">6.3.</span> <span class="post-toc-text">数据卷容器</span></a></li></ol></li><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#Docker-中的网络功能介绍"><span class="post-toc-number">7.</span> <span class="post-toc-text">Docker 中的网络功能介绍</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#外部访问容器"><span class="post-toc-number">7.1.</span> <span class="post-toc-text">外部访问容器</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#容器互联"><span class="post-toc-number">7.2.</span> <span class="post-toc-text">容器互联</span></a></li></ol></li></ol>
        </nav>
    </aside>
    
<article id="post-Docker"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">Docker</h1>
        <div class="post-meta">
            <time class="post-time" title="2017-08-17 09:10:08" datetime="2017-08-17T01:10:08.000Z"  itemprop="datePublished">2017-08-17</time>

            
	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/工具/">工具</a></li></ul>



            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <h1 id="Docker安装"><a href="#Docker安装" class="headerlink" title="Docker安装"></a>Docker安装</h1><h2 id="CentOS7安装Docker"><a href="#CentOS7安装Docker" class="headerlink" title="CentOS7安装Docker"></a>CentOS7安装Docker</h2><p>使用阿里云脚本安装／升级你的Docker客户端</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">curl -sSL http://acs-public-mirror.oss-cn-hangzhou.aliyuncs.com/docker-engine/internet | sh -</div></pre></td></tr></table></figure>
<h2 id="如何使用Docker加速器"><a href="#如何使用Docker加速器" class="headerlink" title="如何使用Docker加速器"></a>如何使用Docker加速器</h2><p>去阿里Docker镜像仓库注册账号：<a href="https://cr.console.aliyun.com" target="_blank" rel="external">https://cr.console.aliyun.com</a></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">sudo mkdir -p /etc/docker</div><div class="line">sudo tee /etc/docker/daemon.json &lt;&lt;-&apos;EOF&apos;</div><div class="line">&#123;</div><div class="line">  &quot;registry-mirrors&quot;: [&quot;https://opvjtsmw.mirror.aliyuncs.com&quot;]</div><div class="line">&#125;</div><div class="line">EOF</div><div class="line">sudo systemctl daemon-reload</div><div class="line">sudo systemctl restart docker</div></pre></td></tr></table></figure>
<h1 id="Docker镜像"><a href="#Docker镜像" class="headerlink" title="Docker镜像"></a>Docker镜像</h1><h2 id="获取Docker镜像"><a href="#获取Docker镜像" class="headerlink" title="获取Docker镜像"></a>获取Docker镜像</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">docker pull ubuntu:14.04</div></pre></td></tr></table></figure>
<h2 id="运行"><a href="#运行" class="headerlink" title="运行"></a>运行</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">docker run -it --rm ubuntu:14.04 bash</div></pre></td></tr></table></figure>
<p>==docker run==  就是运行容器的命令。</p>
<p>==-it==  ：这是两个参数，一个是  ==-i==  ：交互式操作，一个是  ==-t==  终端。</p>
<p>==–rm==  ：这个参数是说容器退出后随之将其删除。</p>
<p>==ubuntu:14.04==  ：这是指用  ubuntu:14.04  镜像为基础来启动容器。</p>
<p>==bash==  ：放在镜像名后的是命令，这里我们希望有个交互式 Shell，因此用的<br>是  bash  。</p>
<p>==exit== ：退出容器</p>
<h2 id="列出镜像"><a href="#列出镜像" class="headerlink" title="列出镜像"></a>列出镜像</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">docker images</div></pre></td></tr></table></figure>
<p>列表包含了仓库名、标签、镜像 ID、创建时间以及所占用的空间。</p>
<h2 id="虚悬镜像"><a href="#虚悬镜像" class="headerlink" title="虚悬镜像"></a>虚悬镜像</h2><p>镜像既没有仓库名，也没有标签，均为  <none>  。</none></p>
<p>一般来说，虚悬镜像已经失去了存在的价值，是可以随意删除的，可以用下面的命令删除。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">docker rmi $(docker images -q -f dangling=true)</div></pre></td></tr></table></figure>
<h2 id="中间层镜像"><a href="#中间层镜像" class="headerlink" title="中间层镜像"></a>中间层镜像</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">docker images -a</div></pre></td></tr></table></figure>
<h2 id="列出部分镜像"><a href="#列出部分镜像" class="headerlink" title="列出部分镜像"></a>列出部分镜像</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">docker images ubuntu</div></pre></td></tr></table></figure>
<h2 id="利用-commit-理解镜像构成"><a href="#利用-commit-理解镜像构成" class="headerlink" title="利用 commit 理解镜像构成"></a>利用 commit 理解镜像构成</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">docker run --name webserver -d -p 80:80 nginx</div></pre></td></tr></table></figure>
<p>这条命令会用  nginx  镜像启动一个容器，命名为  webserver  ，并且映射了 80端口，这样我们可以用浏览器去访问这个  nginx  服务器。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">docker exec -it webserver bash</div><div class="line">echo &apos;&lt;h1&gt;Hello, Docker!&lt;/h1&gt;&apos; &gt; /usr/share/nginx/html/index.html</div></pre></td></tr></table></figure>
<p>我们以交互式终端方式进入  ==webserver==  容器，并执行了  ==bash==  命令，也就是获得一个可操作的 Shell。</p>
<p>然后，我们用  ==</p><h1>Hello, Docker!</h1>==  覆盖了==/usr/share/nginx/html/index.html==  的内容。<p></p>
<p>可以通过  ==docker diff==  命令看到具体的改动。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">docker diff webserver</div></pre></td></tr></table></figure>
<p>我们可以用下面的命令将容器保存为镜像：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">docker commit \</div><div class="line">--author &quot;Tao Wang &lt;twang2218@gmail.com&gt;&quot; \</div><div class="line">--message &quot;修改了默认网页&quot; \</div><div class="line">webserver \</div><div class="line">nginx:v2</div></pre></td></tr></table></figure>
<p>其中  –author  是指定修改的作者，而  –message  则是记录本次修改的内容。</p>
<p>新的镜像定制好后，我们可以来运行这个镜像。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">docker run --name web2 -d -p 81:80 nginx:v2</div></pre></td></tr></table></figure>
<p>这里我们命名为新的服务为  web2  ，并且映射到  81  端口。</p>
<p>使用  docker commit  命令虽然可以比较直观的帮助理解镜像分层存储的概念，<br>但是实际环境中并不会这样使用。</p>
<h2 id="使用-Dockerfile-定制镜像"><a href="#使用-Dockerfile-定制镜像" class="headerlink" title="使用 Dockerfile 定制镜像"></a>使用 Dockerfile 定制镜像</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">$ mkdir mynginx</div><div class="line">$ cd mynginx</div><div class="line">$ touch Dockerfile</div></pre></td></tr></table></figure>
<p>其内容为：</p>
<pre><code>FROM nginx
RUN echo &apos;Hello, Docker!&apos; &gt; /usr/share/nginx/html/index.html
</code></pre><p>构建这个镜像</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ docker build -t nginx:v3 .</div></pre></td></tr></table></figure>
<p>docker build  还支持从 URL 构建，比如可以直接从 Git repo 中构建：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ docker build https://github.com/twang2218/gitlab-ce-zh/tree/master/9.4</div></pre></td></tr></table></figure>
<h2 id="Dockerfile-指令详解"><a href="#Dockerfile-指令详解" class="headerlink" title="Dockerfile 指令详解"></a>Dockerfile 指令详解</h2><h3 id="FROM-指定基础镜像，一个-Dockerfile-中-FROM-是必备的指令，并且必须是第一条指令。"><a href="#FROM-指定基础镜像，一个-Dockerfile-中-FROM-是必备的指令，并且必须是第一条指令。" class="headerlink" title="FROM 指定基础镜像，一个  Dockerfile  中  FROM  是必备的指令，并且必须是第一条指令。"></a>FROM 指定基础镜像，一个  Dockerfile  中  FROM  是必备的指令，并且必须是第一条指令。</h3><h3 id="RUN-指令是用来执行命令行命令的。"><a href="#RUN-指令是用来执行命令行命令的。" class="headerlink" title="RUN  指令是用来执行命令行命令的。"></a>RUN  指令是用来执行命令行命令的。</h3><pre><code>shell 格式： RUN &lt;命令&gt;  ，就像直接在命令行中输入的命令一样。

exec 格式： RUN [&quot;可执行文件&quot;, &quot;参数1&quot;, &quot;参数2&quot;]  ，这更像是函数调用中
的格式。
</code></pre><h3 id="COPY-复制文件"><a href="#COPY-复制文件" class="headerlink" title="COPY 复制文件"></a>COPY 复制文件</h3><pre><code>格式：

COPY &lt;源路径&gt;... &lt;目标路径&gt;

COPY [&quot;&lt;源路径1&gt;&quot;,... &quot;&lt;目标路径&gt;&quot;]

COPY package.json /usr/src/app/
</code></pre><h3 id="ADD-更高级的复制文件"><a href="#ADD-更高级的复制文件" class="headerlink" title="ADD 更高级的复制文件"></a>ADD 更高级的复制文件</h3><pre><code>ADD  指令和  COPY  的格式和性质基本一致。

所有的文件复制均使用  COPY  指令，仅在需要自动解压缩的场合使用  ADD  。
</code></pre><h3 id="CMD-容器启动命令"><a href="#CMD-容器启动命令" class="headerlink" title="CMD 容器启动命令"></a>CMD 容器启动命令</h3><pre><code>CMD  指令的格式和  RUN  相似，也是两种格式：
shell  格式： CMD &lt;命令&gt;
exec  格式： CMD [&quot;可执行文件&quot;, &quot;参数1&quot;, &quot;参数2&quot;...]
参数列表格式： CMD [&quot;参数1&quot;, &quot;参数2&quot;...]  。在指定了  ENTRYPOINT  指令后，用  CMD  指定具体的参数。

CMD [&quot;nginx&quot;, &quot;-g&quot;, &quot;daemon off;&quot;]
</code></pre><h3 id="ENTRYPOINT-入口点"><a href="#ENTRYPOINT-入口点" class="headerlink" title="ENTRYPOINT 入口点"></a>ENTRYPOINT 入口点</h3><pre><code>ENTRYPOINT  的格式和  RUN  指令格式一样，分为  exec  格式和  shell  格式。
</code></pre><h3 id="ENV-设置环境变量"><a href="#ENV-设置环境变量" class="headerlink" title="ENV 设置环境变量"></a>ENV 设置环境变量</h3><pre><code>格式有两种：
ENV &lt;key&gt; &lt;value&gt;
ENV &lt;key1&gt;=&lt;value1&gt; &lt;key2&gt;=&lt;value2&gt;...
</code></pre><h3 id="ARG-构建参数"><a href="#ARG-构建参数" class="headerlink" title="ARG 构建参数"></a>ARG 构建参数</h3><pre><code>格式： ARG &lt;参数名&gt;[=&lt;默认值&gt;]
</code></pre><h3 id="VOLUME-定义匿名卷"><a href="#VOLUME-定义匿名卷" class="headerlink" title="VOLUME 定义匿名卷"></a>VOLUME 定义匿名卷</h3><pre><code>格式为：
VOLUME [&quot;&lt;路径1&gt;&quot;, &quot;&lt;路径2&gt;&quot;...]
VOLUME &lt;路径&gt;
</code></pre><h3 id="EXPOSE-声明端口"><a href="#EXPOSE-声明端口" class="headerlink" title="EXPOSE 声明端口"></a>EXPOSE 声明端口</h3><pre><code>格式为  EXPOSE &lt;端口1&gt; [&lt;端口2&gt;...]  。
</code></pre><h3 id="WORKDIR-指定工作目录"><a href="#WORKDIR-指定工作目录" class="headerlink" title="WORKDIR 指定工作目录"></a>WORKDIR 指定工作目录</h3><pre><code>格式为  WORKDIR &lt;工作目录路径&gt;  。
</code></pre><h3 id="USER-指定当前用户"><a href="#USER-指定当前用户" class="headerlink" title="USER 指定当前用户"></a>USER 指定当前用户</h3><pre><code>格式： USER &lt;用户名&gt;
</code></pre><h3 id="HEALTHCHECK-健康检查"><a href="#HEALTHCHECK-健康检查" class="headerlink" title="HEALTHCHECK 健康检查"></a>HEALTHCHECK 健康检查</h3><pre><code>格式：
HEALTHCHECK [选项] CMD &lt;命令&gt;  ：设置检查容器健康状况的命令
HEALTHCHECK NONE  ：如果基础镜像有健康检查指令，使用这行可以屏蔽掉其健康检查指令
</code></pre><h3 id="ONBUILD-为他人做嫁衣裳"><a href="#ONBUILD-为他人做嫁衣裳" class="headerlink" title="ONBUILD 为他人做嫁衣裳"></a>ONBUILD 为他人做嫁衣裳</h3><h2 id="删除本地镜像"><a href="#删除本地镜像" class="headerlink" title="删除本地镜像"></a>删除本地镜像</h2><pre><code>docker rmi [选项] &lt;镜像1&gt; [&lt;镜像2&gt; ...]
docker rmi $(docker images -q redis)
docker rmi $(docker images -q -f dangling=true)
docker rmi $(docker images -q -f before=mongo:3.2)
</code></pre><p>如果有用这个镜像启动的容器存在（即使容器没有运行），那么同样不可以删除这个镜像。</p>
<p>如果这些容器是不需要的，应该先将它们删除，然后再来删除镜像。</p>
<h1 id="Docker容器"><a href="#Docker容器" class="headerlink" title="Docker容器"></a>Docker容器</h1><h2 id="启动容器"><a href="#启动容器" class="headerlink" title="启动容器"></a>启动容器</h2><p>例如，下面的命令输出一个 “Hello World”，之后终止容器。</p>
<pre><code>$ sudo docker run ubuntu:14.04 /bin/echo &apos;Hello world&apos;
</code></pre><p>下面的命令则启动一个 bash 终端，允许用户进行交互。</p>
<pre><code>sudo docker run -t -i ubuntu:14.04 /bin/bash
</code></pre><h2 id="后台-background-运行"><a href="#后台-background-运行" class="headerlink" title="后台(background)运行"></a>后台(background)运行</h2><p>可以通过添加  -d  参数来实现</p>
<p>==docker ps==  命令来查看容器信息。</p>
<p>要获取容器的输出信息，可以通过  ==docker logs==  命令。</p>
<p>可以使用  ==docker stop==  来终止一个运行中的容器。</p>
<p>终止状态的容器可以用  ==docker ps -a== 命令看到。</p>
<p>处于终止状态的容器，可以通过  ==docker start==  命令来重新启动。</p>
<p>此外，==docker restart== 命令会将一个运行态的容器终止，然后再重新启动它。</p>
<h2 id="进入容器"><a href="#进入容器" class="headerlink" title="进入容器"></a>进入容器</h2><p>attach 命令</p>
<pre><code>docker attach ID/name
</code></pre><h2 id="导出和导入容器"><a href="#导出和导入容器" class="headerlink" title="导出和导入容器"></a>导出和导入容器</h2><p>如果要导出本地某个容器，可以使用  docker export  命令。</p>
<pre><code>sudo docker export 7691a814370e &gt; ubuntu.tar
</code></pre><p>导入容器快照</p>
<pre><code>$ cat ubuntu.tar | sudo docker import - test/ubuntu:v1.0
$ sudo docker images
</code></pre><p>此外，也可以通过指定 URL 或者某个目录来导入，例如</p>
<pre><code>$sudo docker import http://example.com/exampleimage.tgz example/imagerepo
</code></pre><h2 id="删除容器"><a href="#删除容器" class="headerlink" title="删除容器"></a>删除容器</h2><pre><code>$sudo docker rm trusting_newton
</code></pre><p>如果要删除一个运行中的容器，可以添加  -f  参数。Docker 会发送  SIGKILL 信号给容器。</p>
<h2 id="清理所有处于终止状态的容器"><a href="#清理所有处于终止状态的容器" class="headerlink" title="清理所有处于终止状态的容器"></a>清理所有处于终止状态的容器</h2><pre><code>docker rm $(docker ps -a -q)
</code></pre><h1 id="Docker仓库"><a href="#Docker仓库" class="headerlink" title="Docker仓库"></a>Docker仓库</h1><h2 id="安装运行-docker-registry"><a href="#安装运行-docker-registry" class="headerlink" title="安装运行 docker-registry"></a>安装运行 docker-registry</h2><pre><code>$ sudo docker run -d -p 5000:5000 registry
$ sudo docker run -d -p 5000:5000 -v /opt/data/registry:/var/lib/registry registry
</code></pre><h2 id="在私有仓库上传、下载、搜索镜像"><a href="#在私有仓库上传、下载、搜索镜像" class="headerlink" title="在私有仓库上传、下载、搜索镜像"></a>在私有仓库上传、下载、搜索镜像</h2><p>创建好私有仓库之后，就可以使用  docker tag 来标记一个镜像，然后推送它到仓库，别的机器上就可以下载下来了。</p>
<pre><code>sudo docker tag nginx:v3 192.168.0.150:5000/test
</code></pre><p>使用  docker push  上传标记的镜像。</p>
<pre><code>sudo docker push 192.168.0.150:5000/test

curl http://192.168.0.150:5000/v1/search
</code></pre><h1 id="Docker-数据管理"><a href="#Docker-数据管理" class="headerlink" title="Docker 数据管理"></a>Docker 数据管理</h1><h2 id="创建一个数据卷"><a href="#创建一个数据卷" class="headerlink" title="创建一个数据卷"></a>创建一个数据卷</h2><p>在用  docker run  命令的时候，使用  -v  标记来创建一个数据卷并挂载到容器里。在一次 run 中多次使用可以挂载多个数据卷。<br>下面创建一个名为 web 的容器，并加载一个数据卷到容器的  /webapp  目录。</p>
<pre><code>$ sudo docker run -d -P --name web -v /webapp training/webapp python app.py
</code></pre><h2 id="删除数据卷"><a href="#删除数据卷" class="headerlink" title="删除数据卷"></a>删除数据卷</h2><p>可以在删除容器的时候使用  ==docker rm -v==  这个命令。</p>
<p>使用  -v  标记也可以指定挂载一个本地主机的目录到容器中去。</p>
<pre><code>$ sudo docker run -d -P --name web -v /src/webapp:/opt/webapp training/webapp python app.py
</code></pre><h2 id="数据卷容器"><a href="#数据卷容器" class="headerlink" title="数据卷容器"></a>数据卷容器</h2><p>首先，创建一个名为 dbdata 的数据卷容器：</p>
<pre><code>$ sudo docker run -d -v /dbdata --name dbdata training/postgres echo Data-only container for postgres
</code></pre><p>然后，在其他容器中使用  –volumes-from  来挂载 dbdata 容器中的数据卷。</p>
<pre><code>$ sudo docker run -d --volumes-from dbdata --name db1 training/postgres
$ sudo docker run -d --volumes-from dbdata --name db2 training/postgres
</code></pre><h1 id="Docker-中的网络功能介绍"><a href="#Docker-中的网络功能介绍" class="headerlink" title="Docker 中的网络功能介绍"></a>Docker 中的网络功能介绍</h1><h2 id="外部访问容器"><a href="#外部访问容器" class="headerlink" title="外部访问容器"></a>外部访问容器</h2><pre><code>$ sudo docker run -d -p 5000:5000 training/webapp python app.py
</code></pre><h2 id="容器互联"><a href="#容器互联" class="headerlink" title="容器互联"></a>容器互联</h2><p>创建一个新的 web 容器，并将它连接到 db 容器</p>
<pre><code>$ sudo docker run -d -P --name web --link db:db training/webapp python app.py
</code></pre>
        </div>

        <blockquote class="post-copyright">
    <div class="content">
        
<span class="post-time">
    Last updated: <time datetime="2017-08-18T09:19:31.049Z" itemprop="dateUpdated">2017-08-18 17:19:31</time>
</span><br>


        
    </div>
    <footer>
        <a href="http://koushen001.oschina.io">
            <img src="/img/avatar.jpg" alt="刺客">
            刺客
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Docker/">Docker</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://koushen001.oschina.io/2017/08/17/Docker/&title=《Docker》 — 刺客&pic=http://koushen001.oschina.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://koushen001.oschina.io/2017/08/17/Docker/&title=《Docker》 — 刺客&source=学无止境" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://koushen001.oschina.io/2017/08/17/Docker/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《Docker》 — 刺客&url=http://koushen001.oschina.io/2017/08/17/Docker/&via=http://koushen001.oschina.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://koushen001.oschina.io/2017/08/17/Docker/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2017/08/18/常用软件/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">常用软件</h4>
      </a>
    </div>
  

  
</nav>



    














</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        欢迎常来~
        <i class="icon icon-quote-right"></i>
    </h3>
    <div class="reward-content">
        
        <div class="reward-code">
            <img id="rewardCode" src="/img/wechat.jpg" alt="打赏二维码">
        </div>
        
        <label class="reward-toggle">
            <input id="rewardToggle" type="checkbox" class="reward-toggle-check"
                data-wechat="/img/wechat.jpg" data-alipay="/img/alipay.jpg">
            <div class="reward-toggle-ctrol">
                <span class="reward-toggle-item wechat">微信</span>
                <span class="reward-toggle-label"></span>
                <span class="reward-toggle-item alipay">支付宝</span>
            </div>
        </label>
        
    </div>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            
                <span><a href="/atom.xml" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            
            <span>This blog is licensed under a <a rel="license" href="https://creativecommons.org/licenses/by/4.0/">Creative Commons Attribution 4.0 International License</a>.</span>
        </p>
    </div>
    <div class="bottom">
        <p><span>刺客 &copy; 2017</span>
            <span>
                
                Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a>
            </span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://koushen001.oschina.io/2017/08/17/Docker/&title=《Docker》 — 刺客&pic=http://koushen001.oschina.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://koushen001.oschina.io/2017/08/17/Docker/&title=《Docker》 — 刺客&source=学无止境" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://koushen001.oschina.io/2017/08/17/Docker/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《Docker》 — 刺客&url=http://koushen001.oschina.io/2017/08/17/Docker/&via=http://koushen001.oschina.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://koushen001.oschina.io/2017/08/17/Docker/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };


</script>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/search.min.js" async></script>






<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>



<script>
(function() {
    var OriginTitile = document.title, titleTime;
    document.addEventListener('visibilitychange', function() {
        if (document.hidden) {
            document.title = '死鬼去哪里了！';
            clearTimeout(titleTime);
        } else {
            document.title = '(つェ⊂)咦!又好了!';
            titleTime = setTimeout(function() {
                document.title = OriginTitile;
            },2000);
        }
    });
})();
</script>



</body>
</html>
